/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FramModelo.java
 *
 * Created on Dec 6, 2011, 6:13:43 PM
 */
package ar.com.wholeconsulting.pacc.gui.modelos;

import ar.com.wholeconsulting.pacc.gui.utils.frame.SaveOnCloseDialog;
import ar.com.wholeconsulting.pacc.gui.utils.list.ElementosListModel;
import ar.com.wholeconsulting.pacc.gui.modelos.calculos.ElementosGui;
import ar.com.wholeconsulting.pacc.gui.utils.treetable.generic.GenericEditorItems;
import ar.com.wholeconsulting.pacc.gui.utils.treetable.generic.GenericTreeTableModel;
import ar.com.wholeconsulting.pacc.model.Fabricante;
import ar.com.wholeconsulting.pacc.model.Fluido;
import ar.com.wholeconsulting.pacc.model.Modelo;
import ar.com.wholeconsulting.pacc.persistance.JPASession;
import java.awt.Window;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultCellEditor;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author pable
 */
public class FrameModelo extends SaveOnCloseDialog {

    private Modelo currentModelo;

    /**
     * Creates new form FramModelo
     */
    public FrameModelo(JFrame frame) {
        super(frame, true);
        isNew = true;
        currentModelo = new Modelo();
        this.initComponents();
        this.initComponetsView();

    }

    public FrameModelo(JFrame frame, Modelo modelo) {
        super(frame, true);
        currentModelo = JPASession.getInstance().geGenericstSingleResultNamedQuery(Modelo.class, Modelo.FIND_BY_ID, modelo.getId());
        this.initComponents();
        this.initComponetsView();
        this.initComponetsViewFixed();
    }

    private void initComponetsView() {
        List<Object> list = JPASession.getInstance().getResultListNamedQuery(Fabricante.FIND_ALL);
        for (Object fabricante : list) {
            this.comboFabricante.addItem(fabricante);
        }
        comboFabricante.setSelectedItem(currentModelo.getFabricante());
        list = JPASession.getInstance().getResultListNamedQuery(Fluido.FIND_ALL);
        for (Object fluido : list) {
            this.comboFluido.addItem(fluido);
        }

        for (ElementosGui ele : ElementosGui.values()) {
            comboElementos.addItem(ele);
        }
        initTreeAtributos();
        listModelo.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                boolean enable = false;
                if (e.getFirstIndex() != -1) {
                    enable = true;
                }
                buttonDelElemento.setEnabled(enable);
                buttonEditElemento.setEnabled(enable);
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        comboFluido = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        comboFabricante = new javax.swing.JComboBox();
        textModelo = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        textDescripcion = new javax.swing.JTextField();
        buttonCancelar = new javax.swing.JButton();
        buttonGuardar = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        panelConfig = new javax.swing.JPanel();
        comboElementos = new javax.swing.JComboBox();
        labelDisposicion = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listModelo = new javax.swing.JList(new ElementosListModel(currentModelo));
        comboDisposicion = new javax.swing.JComboBox();
        buttonAddElemento = new javax.swing.JButton();
        buttonDelElemento = new javax.swing.JButton();
        buttonCalcularModelo = new javax.swing.JButton();
        buttonEditElemento = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        labelCapacidad = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        labelDesarrollo = new javax.swing.JLabel();
        labelPeso = new javax.swing.JLabel();
        textCapacidadNominal = new javax.swing.JTextField();
        buttonCalcularCaracteristicas = new javax.swing.JButton();
        panelDatos = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        panelApoyos = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        treeTableAtributos = new org.jdesktop.swingx.JXTreeTable();

        setTitle("Modelos");
        setModalExclusionType(java.awt.Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        jLabel3.setText("Fluido Contenido:");

        comboFluido.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                comboFluidoItemStateChanged(evt);
            }
        });

        jLabel4.setText("Fabricante");

        jLabel5.setText("Modelo");

        jLabel1.setText("Descripcion");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)))
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(comboFluido, 0, 260, Short.MAX_VALUE)
                    .addComponent(comboFabricante, 0, 260, Short.MAX_VALUE)
                    .addComponent(textModelo, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                    .addComponent(textDescripcion, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(comboFabricante))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboFluido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
                    .addComponent(textDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        buttonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aspneticons_v1.0_Nov2006/trash_16x16.gif"))); // NOI18N
        buttonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelarActionPerformed(evt);
            }
        });

        buttonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aspneticons_v1.0_Nov2006/save_16x16.gif"))); // NOI18N
        buttonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonGuardarActionPerformed(evt);
            }
        });

        labelDisposicion.setText("Disposicion");

        jScrollPane1.setViewportView(listModelo);

        comboDisposicion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "HORIZONTAL", "VERTICAL" }));

        buttonAddElemento.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aspneticons_v1.0_Nov2006/add_16x16.gif"))); // NOI18N
        buttonAddElemento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAddElementoActionPerformed(evt);
            }
        });

        buttonDelElemento.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aspneticons_v1.0_Nov2006/delete_16x16.gif"))); // NOI18N
        buttonDelElemento.setEnabled(false);

        buttonCalcularModelo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/16x16/calculator.png"))); // NOI18N

        buttonEditElemento.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aspneticons_v1.0_Nov2006/edit_16x16.gif"))); // NOI18N
        buttonEditElemento.setEnabled(false);
        buttonEditElemento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEditElementoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelConfigLayout = new javax.swing.GroupLayout(panelConfig);
        panelConfig.setLayout(panelConfigLayout);
        panelConfigLayout.setHorizontalGroup(
            panelConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelConfigLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(comboElementos, 0, 198, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelConfigLayout.createSequentialGroup()
                        .addComponent(buttonAddElemento)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonEditElemento)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonDelElemento)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonCalcularModelo))
                    .addComponent(labelDisposicion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(comboDisposicion, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        panelConfigLayout.setVerticalGroup(
            panelConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelConfigLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboElementos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelDisposicion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelConfigLayout.createSequentialGroup()
                        .addComponent(comboDisposicion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(panelConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(buttonAddElemento)
                            .addComponent(buttonEditElemento)
                            .addComponent(buttonDelElemento)
                            .addComponent(buttonCalcularModelo))
                        .addGap(210, 210, 210))
                    .addGroup(panelConfigLayout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );

        jTabbedPane1.addTab("Configuracion del Modelo", panelConfig);

        jLabel10.setText("Capacidad:");

        jLabel11.setText("Desarrollo:");

        jLabel12.setText("Peso:");

        labelCapacidad.setBackground(java.awt.Color.pink);
        labelCapacidad.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        labelCapacidad.setOpaque(true);

        jLabel16.setText("Capacidad");

        jLabel17.setText("Nominal");

        labelDesarrollo.setBackground(java.awt.Color.pink);
        labelDesarrollo.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        labelDesarrollo.setOpaque(true);

        labelPeso.setBackground(java.awt.Color.pink);
        labelPeso.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        labelPeso.setOpaque(true);

        buttonCalcularCaracteristicas.setIcon(new javax.swing.ImageIcon(getClass().getResource("/16x16/calculator.png"))); // NOI18N

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel12, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel10)
                        .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(12, 12, 12)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelPeso, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel5Layout.createSequentialGroup()
                            .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jLabel17, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel5Layout.createSequentialGroup()
                            .addComponent(labelDesarrollo, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(buttonCalcularCaracteristicas))
                        .addGroup(jPanel5Layout.createSequentialGroup()
                            .addComponent(labelCapacidad, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(textCapacidadNominal, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE))))
                .addContainerGap(160, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(jLabel17))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(textCapacidadNominal)
                    .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(labelCapacidad, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonCalcularCaracteristicas)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
                            .addComponent(labelDesarrollo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(labelPeso, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE))))
                .addGap(61, 61, 61))
        );

        jTabbedPane1.addTab("Otras Caracteristicas", jPanel5);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Presion max admisible", null},
                {"Temp max consid", null},
                {"Norma de apliacion", null},
                {"Servicio", null}
            },
            new String [] {
                "Atributo", "Valor"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.getTableHeader().setReorderingAllowed(false);
        panelDatos.setViewportView(jTable1);

        jTabbedPane1.addTab("Datos para el Calculo del Modelo", panelDatos);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Tipo", null},
                {"Dimensiones", null},
                {"Material", null}
            },
            new String [] {
                "Atributo", "Valor"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable2.getTableHeader().setReorderingAllowed(false);
        panelApoyos.setViewportView(jTable2);

        jTabbedPane1.addTab("Apoyos y Material", panelApoyos);

        treeTableAtributos.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(treeTableAtributos);

        jTabbedPane1.addTab("Datos", jScrollPane2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonCancelar)
                        .addGap(8, 8, 8)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonGuardar)
                    .addComponent(buttonCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-479)/2, (screenSize.height-446)/2, 479, 446);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonGuardarActionPerformed
        ok = true;
        this.setVisible(false);
    }//GEN-LAST:event_buttonGuardarActionPerformed

    private void buttonEditElementoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEditElementoActionPerformed
        Object elemento = listModelo.getSelectedValue();
        if (elemento != null) {
            try {
                Class frame = ElementosGui.fromClass(elemento.getClass());
                Constructor constructor = frame.getConstructor(JDialog.class, Modelo.class, elemento.getClass());
                Window jframe = (Window) constructor.newInstance(this, currentModelo, elemento);
                jframe.setVisible(true);
            } catch (InstantiationException ex) {
                Logger.getLogger(FrameModelo.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(FrameModelo.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(FrameModelo.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvocationTargetException ex) {
                Logger.getLogger(FrameModelo.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchMethodException ex) {
                Logger.getLogger(FrameModelo.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(FrameModelo.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchFieldException ex) {
                Logger.getLogger(FrameModelo.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
}//GEN-LAST:event_buttonEditElementoActionPerformed

    private void buttonAddElementoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddElementoActionPerformed
        try {
            if (comboElementos.getSelectedIndex() != -1) {
                Class clazz = ((ElementosGui) comboElementos.getSelectedItem()).getFrame();
                Constructor c = clazz.getConstructor(JDialog.class, Modelo.class);
                Window frame = (Window) c.newInstance(this, currentModelo);
                frame.setVisible(true);
                listModelo.updateUI();
            }
        } catch (InstantiationException ex) {
            Logger.getLogger(FrameModelo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(FrameModelo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(FrameModelo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(FrameModelo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(FrameModelo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(FrameModelo.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_buttonAddElementoActionPerformed

    private void buttonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelarActionPerformed
        ok = false;
        this.setVisible(false);
    }//GEN-LAST:event_buttonCancelarActionPerformed

    private void comboFluidoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_comboFluidoItemStateChanged
        if (comboFluido.getSelectedIndex() != -1) {
            currentModelo.setFluido((Fluido) comboFluido.getSelectedItem());
        }
    }//GEN-LAST:event_comboFluidoItemStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAddElemento;
    private javax.swing.JButton buttonCalcularCaracteristicas;
    private javax.swing.JButton buttonCalcularModelo;
    private javax.swing.JButton buttonCancelar;
    private javax.swing.JButton buttonDelElemento;
    private javax.swing.JButton buttonEditElemento;
    private javax.swing.JButton buttonGuardar;
    private javax.swing.JComboBox comboDisposicion;
    private javax.swing.JComboBox comboElementos;
    private javax.swing.JComboBox comboFabricante;
    private javax.swing.JComboBox comboFluido;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JLabel labelCapacidad;
    private javax.swing.JLabel labelDesarrollo;
    private javax.swing.JLabel labelDisposicion;
    private javax.swing.JLabel labelPeso;
    private javax.swing.JList listModelo;
    private javax.swing.JScrollPane panelApoyos;
    private javax.swing.JPanel panelConfig;
    private javax.swing.JScrollPane panelDatos;
    private javax.swing.JTextField textCapacidadNominal;
    private javax.swing.JTextField textDescripcion;
    private javax.swing.JTextField textModelo;
    private org.jdesktop.swingx.JXTreeTable treeTableAtributos;
    // End of variables declaration//GEN-END:variables

    private void initComponetsViewFixed() {
        this.setTitle("Editando modelo: " + currentModelo.getNombre());
        comboFluido.setSelectedItem(currentModelo.getFluido());
        comboFabricante.setSelectedItem(currentModelo.getFabricante());
        comboFabricante.setEnabled(false);
        textModelo.setText(currentModelo.getNombre());
        textModelo.setEnabled(false);
        textDescripcion.setText(currentModelo.getDescripcion());
    }

    private void initTreeAtributos() {
        GenericTreeTableModel model = new GenericTreeTableModel(treeTableAtributos);
        List<GenericEditorItems> list = new LinkedList<GenericEditorItems>();
        list.add(new GenericEditorItems("Datos Para el Calculo del Modelo",
                new String[]{"Presion max admisible", "Temperatura max consid", /*
                 * "Norma de apliacion", "Servicio"
                 */},
                new String[]{"PresionMaxima", "temperaturaMaxima",/*
                 * "l1", "r1", "r0"
                 */},
                new DefaultCellEditor[]{null, null/*
                 * , null, null, null
                 */

                }, currentModelo));

//        list.add(new GenericEditorItems("Soldaduras y Eficiencia de Juntas",
//                new String[]{"Soldadura del Disco", "Radio del Disco", "Eficiencia de Junta", "Tipo de Soldadura Circulo", "Metodo de Soldadura Circulo", "Radio de Soldadura Circulo"},
//                new String[]{"SoldaduraInterna", "RadiografiaInterna", "EficienciaDeJunta", "TipoSoldadura", "MetodoSoldaduraExterna", "RadiografiaExterna"},
//                new DefaultCellEditor[]{new DefaultCellEditor(new JComboBox(Soldadura.values())), new DefaultCellEditor(new JComboBox(RadiografiaSoldadura.values())), null,
//                    new DefaultCellEditor(new JComboBox(Soldadura.values())),
//                    new DefaultCellEditor(new JComboBox(MetodoSoldadura.values())),
//                    new DefaultCellEditor(new JComboBox(RadiografiaSoldadura.values()))}, currentCabezal));
//        list.add(new GenericEditorItems("Otras Caracteristicas",
//                new String[]{"Tension Admisible", "Material"},
//                new String[]{"tensionAdmisible", "acero"},
//                new DefaultCellEditor[]{null,
//                    new DefaultCellEditor(new JComboBox(JPASession.getInstance().getResultListNamedQuery(Acero.FIND_ALL).toArray()))}, currentCabezal));
        model.initTreeTable(list);
        treeTableAtributos.updateUI();
    }

    @Override
    protected void saveNewObjects() {
        if (isNew) {
            JPASession.getInstance().getManager().persist(currentModelo);
        }
    }
}
